import React from "react";
import {View} from "react-native";
import InputForm from "../components/form/InputForm";
import {globalStore} from "../libs/ShareStore";
import ANavigationComponent, {ANavigationComponentProp} from "../components/navigator/ANavigationComponent";
import {pageStyle} from "./style/pageStyle";
import EButton from "../components/utils/EButton";
import {FileInfo} from "../libs/sdk/echatim/core/fileclient/FileServerClient";
import EChatIMApis from "../libs/sdk/echatim/core/apis/EChatIMApis";
import Beans from "../libs/sdk/echatim/common/utils";
import AvatarHeader from "../components/utils/AvatarHeader";


type ModifyUserInfoModalProp = {

} & ANavigationComponentProp

type ModifyUserInfoPageState = {
    avatar:string
}

export default class ModifyUserInfoPage extends ANavigationComponent<ModifyUserInfoModalProp, ModifyUserInfoPageState>  {
    constructor(props:any) {
        super(props);
        // const auid = this.getNavigationParam('auid');
        // console.log(`ready to modify user info :${auid}`);
        this.state = {avatar:globalStore.userInfo.avatar};
    }


    onModifyUserInfo():void{
        globalStore.onModifyUserInfoPressOk()
    }


    onAvatarUpload(file:FileInfo){
        globalStore.userInfo.avatar = file.url;
        EChatIMApis.userUpdate.call(Beans.to(globalStore.userInfo)).then((resp) => {
            globalStore.showToast("修改成功");
        }).catch((e) => {
            globalStore.showApiErrorToast(e);
        })
    }

    render() {
        return (
            <View style={pageStyle.container}>
                <AvatarHeader avatar={globalStore.userInfo.avatar}
                              headerFirstText={globalStore.userInfo.name}
                              headerSecondText={globalStore.userInfo.auid}
                              showEditAvatar={true}
                              onAvatarUpload={(info)=>this.onAvatarUpload(info)} />
                <InputForm startText={'生日:'} defaultValue={globalStore.userInfo.birth} onChangeInput={(text)=>{globalStore.userInfo.birth = text}} />
                <InputForm startText={'手机:'}  defaultValue={globalStore.userInfo.mobile} onChangeInput={(text)=>{globalStore.userInfo.mobile = text}} />
                <InputForm startText={'邮箱:'}  defaultValue={globalStore.userInfo.email} onChangeInput={(text)=>{globalStore.userInfo.email = text}} />
                <InputForm startText={'签名:'}  defaultValue={globalStore.userInfo.sign} onChangeInput={(text)=>{globalStore.userInfo.sign = text}} />
                <EButton title={'编辑信息'} onPress={()=>{this.onModifyUserInfo()}}/>
            </View>
        )
     }

     componentWillMount(): void {

     }
}
